package com.greenleaf.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.greenleaf.entity.Address;
import com.greenleaf.entity.Order;
import com.greenleaf.entity.OrderDetail;
import com.greenleaf.entity.PdtDetail;
import com.greenleaf.entity.PdtSize;
import com.greenleaf.entity.SearchCon;
import com.greenleaf.entity.Vip;
import com.greenleaf.service.AddressService;
import com.greenleaf.service.OrderDetailService;
import com.greenleaf.service.OrderService;
import com.greenleaf.service.PdtDetailService;
import com.greenleaf.service.PdtSizeService;
import com.greenleaf.util.DateUtils;
import com.greenleaf.util.PayUtil;

@Controller
@RequestMapping("/order")
public class OrderController {
	@Resource
	private PayUtil payUtil;
	@Resource
	private OrderService orderService;
	@Resource
	private PdtSizeService pdtSizeService;
	@Resource
	private AddressService addressService;
	@Resource
	private PdtDetailService pdtDetailService;
	@Resource
	private OrderDetailService orderDetailService;
	
	@RequestMapping("/getAll")
	@ResponseBody
	//http://localhost:8080/Greenleaf/order/getAll
	public Object getAllOrder(SearchCon searchCon) {
		List<Order> user = orderService.getAllOrder(searchCon);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", "0");
		map.put("msg", "0");
		map.put("count",orderService.getTotalOrder(searchCon));
		map.put("data", user);
		return map;
	}
	
	@RequestMapping("/getAllOrderDetail")
	@ResponseBody
	//http://localhost:8080/Greenleaf/order/getAllOrderDetail
	public Object getAllOrderDetail(SearchCon searchCon) {
		List<OrderDetail> list = orderDetailService.getAllOrderDetail(searchCon);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", "0");
		map.put("msg", "0");
		map.put("count",orderDetailService.getTotalOrderDetail(searchCon));
		map.put("data", list);
		return map;
	}
	
	@RequestMapping("/getOrdetByState")
	@ResponseBody
	//http://localhost:8080/Greenleaf/order/getOrdetByState
	public Object getOrdetByState(String state) {
		
		Order order=new Order();
		switch (state) {
		case "未支付":
			order.setPaymentState("未支付");
			break;
		case "待发货":
			order.setMasterState("待发货");
			break;
		case "待收货":
			order.setMasterState("待收货");
			break;
		case "待评价":
			order.setMasterState("待评价");
			break;
		case "已评价":
			order.setMasterState("已评价");
			break;
		}
		List<Order> list = orderService.getOrdetByState(order);
		Map<String, Object> map = new HashMap<String, Object>();
		boolean count=true;
		if(list.size()==0) {
			count=false;
		}
		map.put("code", "0");
		map.put("msg", "0");
		map.put("count",count);
		map.put("data", list);
		return map;
	}
	
	/**
	 * 支付成功之后的回调
	 * @param out_trade_no 订单号
	 * @return
	 *  http://localhost:8080/Greenleaf/order/orderSuccess
	 */
	@RequestMapping("/orderSuccess")
	public String orderSuccess(String out_trade_no) {
		//通过订单号更新订单状态为已支付
		Order order=new Order(null, out_trade_no, null, null, 0, null, "已支付", "待发货", "未取消", new Date(), "支付宝", null);
		orderService.updateOrderByNumber(order);
		return "redirect:/index.jsp"; //回到商城首页
	}
	
	@RequestMapping("/getForm")
	@ResponseBody // http://localhost:8080/Greenleaf/order/getForm
	public Object getTotalOrder(String orderNumber) {
		System.out.println(orderNumber);System.out.println(orderNumber);
		Order ordet=orderService.getOrderByNumber(orderNumber);
		System.out.println(ordet);System.out.println(ordet);
		System.out.println(ordet);System.out.println(ordet);
		String Total=ordet.getTotal()+"";
		String form=payUtil.alipay(ordet.getOrderNumber(), Total, "一片绿叶盆栽", "🍃");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", "0");
		map.put("msg", "0");
		map.put("count", 1);
		map.put("form", form);
		return map;
	}
	
	@RequestMapping("/getTotal")
	@ResponseBody // http://localhost:8080/Greenleaf/order/getTotal
	public Object getTotalOrder(SearchCon searchCon) {
		int user = orderService.getTotalOrder(searchCon);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", "0");
		map.put("msg", "0");
		map.put("count", 1);
		map.put("data", user);
		return map;
	}
	
	@RequestMapping("/address")
	@ResponseBody // http://localhost:8080/Greenleaf/order/address
	public Object getTotalOrder(Integer adsId,String ids,String total,HttpServletRequest request) {
		String name = ids+total+"number";
		
		String ordetNumber=(String) request.getSession().getAttribute(name);
		
		Order order=orderService.getOrderByNumber(ordetNumber);
		Address address=addressService.getAddressById(adsId);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", "0");
		map.put("msg", "0");
		map.put("count", 1);
		map.put("order", order);
		map.put("address", address);
		return map;
	}
	
	
	@RequestMapping("/add")
	@ResponseBody
	//http://localhost:8080/Greenleaf/order/add
	public Object addOrder(Order order,String ids,HttpServletRequest request) {
		//获取用户Id
		Vip vip=(Vip) request.getSession().getAttribute("vipInfo");
		Date date=new Date();
		String ordetNumber=vip.getvId()+DateUtils.formatDateTime(date, "yyyyMMddHHmmss");
		String name=ids+(int)order.getTotal()+"number";
		request.getSession().setAttribute(name, ordetNumber);
		Order or=new Order(null, ordetNumber, vip.getvId(), date, order.getTotal(), order.getOrderText(), "未支付", "待发货", "未取消", null, null, afterOneHourToNowDate(date));
		//添加订单
		int r =orderService.addOrder(or);
		List<Integer> list = new ArrayList<Integer>();
		for (String id : ids.split(",")) {
			list.add(Integer.parseInt(id));
		}
		List<PdtDetail> pdtDetail = pdtDetailService.getPdtDetailByIds(list);
		for (PdtDetail pdtDetail2 : pdtDetail) {
			//删除购物车中购买的商品
			pdtDetailService.delPdtDetail(pdtDetail2.getdId());
			//添加订单详情
			OrderDetail orderDetail=
				new OrderDetail(null, pdtDetail2.getPdtId(), pdtDetail2.getPdtName()
						,pdtDetail2.getUnit() ,pdtDetail2.getNumber(), (int) pdtDetail2.getTotal()
						, ordetNumber, pdtDetail2.getPdtUrl());
			r = orderDetailService.addOrderDetail(orderDetail);
			//减少库存
			PdtSize pdtSize= new PdtSize(null, pdtDetail2.getSize(), pdtDetail2.getPdtId(), null);
			Integer stock = pdtSizeService.getPdtSize(pdtSize);
			stock=stock-pdtDetail2.getNumber();
			pdtSize.setStock(stock+"");
			pdtSizeService.updatePdtSizeByName(pdtSize);
		}
		
		return r==1?true:false;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	//http://localhost:8080/Greenleaf/order/update
	public Object updateOrder(Order order) {
		//System.out.println(order);
		int r = orderService.updateOrder(order);
		return r!=0?true:false;
	}
	
	@RequestMapping("/del")
	@ResponseBody
	//http://localhost:8080/Greenleaf/order/del
	public Object delOrder(int orderId) {
		//System.out.println(pdtId);
		int r =  orderService.delOrder(orderId);
		return  r==1?true:false;
	}
	/**
	 * 获取当前时间后一小时的时间
	 * @param date
	 * @return java.util.Date
	 */
	public Date afterOneHourToNowDate(Date date) {
	    Calendar calendar = Calendar.getInstance();
	    /* HOUR_OF_DAY 指示一天中的小时 */
	    calendar.setTime(date);
	    calendar.add(Calendar.HOUR_OF_DAY, 1);
	    return calendar.getTime();
	}

}
